import { Request, Response } from 'express';
// import moment from '_moment@2.29.1@moment';
import Mock from 'mockjs';
// const Random = Mock.Random;
import DUser from './user_data';
const waitTime = (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

async function getFakeCaptcha(req: Request, res: Response) {
  await waitTime(2000);
  return res.json('captcha-xxx');
}

// 代码中会兼容本地 service mock 以及部署站点的静态数据
export default {
  // 支持值为 Object 和 Array
  'GET /api/currentUser': DUser.currentUser,
  // GET POST 可省略
  'GET /api/users': [
    {
      key: '1',
      name: 'John Brown',
      age: 32,
      address: 'New York No. 1 Lake Park',
    },
    {
      key: '2',
      name: 'Jim Green',
      age: 42,
      address: 'London No. 1 Lake Park',
    },
    {
      key: '3',
      name: 'Joe Black',
      age: 32,
      address: 'Sidney No. 1 Lake Park',
    },
  ],
  // GET POST 可省略
  'GET /api/user-list': (req: Request, res: Response) => {
    let current: number = Number(req.query.current || 1);
    let pageSize: number = Number(req.query.pageSize || 1);
    let name: string = String(req.query.name || '');
    let sorter: any = req.query.sorter || {};
    let filter: any = req.query.filter || {};
    console.log(current, pageSize, name, sorter, filter);
    const mock_data = Mock.mock({
      'list|30-100': [
        {
          'key|+1': 1,
          name: '@cname',
          id: '@id',
          desc: '@cparagraph',
          'section|1': ['内科', '外科', '儿科', '妇产科', '肿瘤科'],
          'technical|1': '医学影像科,检验科,病理科,功能检查科'.split(','),
          // birthday: Random.date('yyyy-MM-dd'),
          birthday: '@date',
          'education|1': '小学、初中、中专、高中、专科、本科、研究生'.split('、'),
          // login_count: Random.integer(10, 1000),
          'login_count|100-10000': 5,
          // login_count: '@integer',
          // registration_time: Random.date('yyyy-MM-dd'),
          registration_time: '@date',
          // last_login_time: Random.date('yyyy-MM-dd'),
          last_login_time: '@date',
          'status|1': [0, 1],
        },
      ],
    });

    mock_data.list = mock_data.list.slice(0, pageSize);
    const data = {
      current,
      data: mock_data.list,
      pageSize,
      success: true,
      total: 100,
    };
    res.send(data);
  },
  'POST /api/login/account': async (req: Request, res: Response) => {
    const { password, userName, type } = req.body;
    await waitTime(2000);
    if (password === 'ant.design' && userName === 'admin') {
      res.send({
        status: 'ok',
        type,
        currentAuthority: 'admin',
      });
      return;
    }
    if (password === 'ant.design' && userName === 'user') {
      res.send({
        status: 'ok',
        type,
        currentAuthority: 'user',
      });
      return;
    }
    if (type === 'mobile') {
      res.send({
        status: 'ok',
        type,
        currentAuthority: 'admin',
      });
      return;
    }

    res.send({
      status: 'error',
      type,
      currentAuthority: 'guest',
    });
  },
  'POST /api/register': (req: Request, res: Response) => {
    res.send({ status: 'ok', currentAuthority: 'user' });
  },
  'GET /api/500': (req: Request, res: Response) => {
    res.status(500).send({
      timestamp: 1513932555104,
      status: 500,
      error: 'error',
      message: 'error',
      path: '/base/category/list',
    });
  },
  'GET /api/404': (req: Request, res: Response) => {
    res.status(404).send({
      timestamp: 1513932643431,
      status: 404,
      error: 'Not Found',
      message: 'No message available',
      path: '/base/category/list/2121212',
    });
  },
  'GET /api/403': (req: Request, res: Response) => {
    res.status(403).send({
      timestamp: 1513932555104,
      status: 403,
      error: 'Unauthorized',
      message: 'Unauthorized',
      path: '/base/category/list',
    });
  },
  'GET /api/401': (req: Request, res: Response) => {
    res.status(401).send({
      timestamp: 1513932555104,
      status: 401,
      error: 'Unauthorized',
      message: 'Unauthorized',
      path: '/base/category/list',
    });
  },

  'GET  /api/login/captcha': getFakeCaptcha,
  // 使用 mockjs 等三方库 表情
  'GET /api/tags': Mock.mock({
    'list|100': [{ name: '@city', 'value|1-100': 50, 'type|0-2': 1 }],
  }),
};
